package com.gce.model;

import com.feilong.lib.javassist.bytecode.FieldInfo;
import com.feilong.lib.javassist.bytecode.MethodInfo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author chengen.gce
 * @DATE 2022/1/22 9:27 下午
 */
public class ClazzInfoDefinition {

    private String classname;
    private String superClassname;
    private List<String> interfaceNameList = new ArrayList<>();
    private List<FieldInfo> fieldInfoList = new ArrayList<>();
    private List<MethodInfo> methodInfoList = new ArrayList<>();

    private Set<String> fieldSet = new HashSet<>();
    private Set<String> methodSet = new HashSet<>();

    public Set<String> getFieldSet() {
        return fieldSet;
    }

    public Set<String> getMethodSet() {
        return methodSet;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public void setSuperClassname(String superClassname) {
        this.superClassname = superClassname;
    }

    public String getClassname() {
        return classname;
    }

    public String getSuperClassname() {
        return superClassname;
    }

    public List<String> getInterfaceNameList() {
        return interfaceNameList;
    }

    public List<FieldInfo> getFieldInfoList() {
        return fieldInfoList;
    }

    public List<MethodInfo> getMethodInfoList() {
        return methodInfoList;
    }

    public String splitSortClassInfo(){
        StringBuilder sb = new StringBuilder();
        sb.append("#classname#").append(classname).append("#superClassname#").append(superClassname);
        if (!interfaceNameList.isEmpty()){
            sb.append("#interface#");
            interfaceNameList.sort(null);
            for (String string : interfaceNameList) {
                sb.append(string).append("|");
            }
        }

        if (!fieldInfoList.isEmpty()){
            sb.append("#field#");
            fieldInfoList.sort(Comparator.comparing(FieldInfo::getName));
            for (FieldInfo fieldInfo : fieldInfoList) {
                sb.append(fieldInfo.getName()).append("|");
                fieldSet.add(fieldInfo.getName());
            }
        }

        if (!methodInfoList.isEmpty()){
            sb.append("#method#");
            methodInfoList.sort(Comparator.comparing(MethodInfo::getName));
            for (MethodInfo methodInfo : methodInfoList) {
                sb.append(methodInfo.getName()).append("|");
                methodSet.add(methodInfo.getName());
            }
        }
        return sb.toString();

    }
}
